package com.zahb.qadx.ui.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseFragmentExt;
import com.zahb.qadx.model.TrainAllBean;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.skeleton.RecyclerViewSkeletonScreen;
import com.zahb.qadx.skeleton.Skeleton;
import com.zahb.qadx.ui.adapter.TrainRecommendAdapter;
import com.zahb.qadx.ui.view.decoration.MColorDrawable;
import com.zahb.qadx.ui.view.decoration.MDividerItemDecoration;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class TrainRecommendListFragment extends BaseFragmentExt implements OnRefreshListener {
    //456
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;


    @BindView(R.id.ll_empty)
    LinearLayoutCompat llEmpty;

    private TrainRecommendAdapter mRecommendAdapter;
    private List<TrainAllBean.RecommendListBean> list = new ArrayList<>();

    public int classid ;

    private MDividerItemDecoration mItemDecoration;
    private RecyclerViewSkeletonScreen mSkeletonScreen;
    private long mSkeletonShowTime;

    @Override
    protected int getLayout() {
        return R.layout.fragment_train_recommend_list;
    }

    @Override
    protected void initViews(View rootView) {
        mUnbinder = ButterKnife.bind(this, rootView);
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setOnRefreshListener(this);
        //updateData(list);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecommendAdapter = new TrainRecommendAdapter(getActivity(),list);
        mRecyclerView.setAdapter(mRecommendAdapter);

        mItemDecoration = new MDividerItemDecoration(getContext());
        MColorDrawable colorDrawable = new MColorDrawable(CompatHelper.getColor(R.color.gray_999999));
        colorDrawable.setIntrinsicWidth(1).setIntrinsicHeight(1);
        mItemDecoration.setDrawable(colorDrawable);
        mItemDecoration.setShowLast(true);
    }

    private boolean mFirstLoad = true;

    @Override
    public void onResume() {
        super.onResume();
        if (mFirstLoad) {
            mFirstLoad = false;
            showSkeleton();
        }
        onRefresh(mRefreshLayout);
    }

    private void updateData(List<TrainAllBean.RecommendListBean> list) {
        this.list.clear();
        this.list.addAll(list);
        mRecommendAdapter.notifyDataSetChanged();
        isVisible(mRecommendAdapter.getItemCount() > 0);
    }

    private void isVisible(boolean flag){
        if (mRecyclerView.getAdapter() instanceof TrainRecommendAdapter) {
            if(flag){
                mRecyclerView.setVisibility(View.VISIBLE);
                llEmpty.setVisibility(View.GONE);
            }else{
                llEmpty.setVisibility(View.VISIBLE);
                mRecyclerView.setVisibility(View.GONE);
            }
        }
    }

    private void showSkeleton() {
        mRefreshLayout.setEnableRefresh(false);
        mSkeletonScreen = Skeleton.bind(mRecyclerView)
                .adapter(mRecommendAdapter)
                .shimmer(true)
                .shimmerMode(RecyclerViewSkeletonScreen.SHIMMER_MODE_ITEM_CHILD)
                .angle(15)
                .frozen(true)
                //.color(R.color.black_333333)
                .duration(1000)
                .count(10)
                .load(R.layout.item_list_curriculum_skeleton)
                .show();
        mRecyclerView.addItemDecoration(mItemDecoration);
        mSkeletonShowTime = System.currentTimeMillis();
    }

    private void hideSkeleton() {
        if (mSkeletonScreen != null) {
            long currentTime = System.currentTimeMillis();
            if (currentTime - mSkeletonShowTime > Constant.MIN_NETWORK_TIME) {
                mRefreshLayout.setEnableRefresh(true);
                mSkeletonScreen.hide();
                mSkeletonScreen = null;
                mRecyclerView.removeItemDecoration(mItemDecoration);
                isVisible(mRecommendAdapter.getItemCount() > 0);
            } else {
                addDisposable(Observable.timer(mSkeletonShowTime + Constant.MIN_NETWORK_TIME - currentTime, TimeUnit.MILLISECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(aLong -> hideSkeleton()));
            }
        }
    }

    private void getTrainDetailsList() {
        addDisposable(RetrofitService.getNetService()
                .getTrainDetailsList(classid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    mRefreshLayout.finishRefresh();
                    mRefreshLayout.finishLoadMore();
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        if(response.getData().getTaskList()!=null&&response.getData().getTaskList().size()>0){
                            updateData(response.getData().getRecommendList());
                        }
                    } else if (response.getStatusCode() != Constant.CODE_OFFLINE) {
                        showBindToast(response.getErrorInfo());
                    }
                    hideSkeleton();
                }, throwable -> {
                    throwable.printStackTrace();
                    mRefreshLayout.finishRefresh();
                    mRefreshLayout.finishLoadMore();
                    Tips.RequestError(getActivity());
                    hideSkeleton();
                }));

    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getTrainDetailsList();
    }
}
